package com.wu;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class AndroidHelloWorldActivity extends Activity {
	/** Called when the activity is first created. */

	Button button1;

	ProgressBar progressBar1;

	int i = 0;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		button1 = (Button) findViewById(R.id.mybutton);
		progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
		button1.setOnClickListener(new MyButtonLisenter());
	}

	class MyButtonLisenter implements OnClickListener {
		@Override
		public void onClick(View arg0) {
			progressBar1.setVisibility(View.VISIBLE);
			System.out.println("start thread");
			i = 0;
			progressBar1.setProgress(i);
			handler.post(runnable);
		}
	}
	
	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			i=i+10;
			Message message = handler.obtainMessage();
			message.arg1=i;
			handler.sendMessage(message);
			
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("message :"+i);
			if (i>=100) {
				System.out.println("remove thread");
				handler.removeCallbacks(runnable);
				progressBar1.setVisibility(View.GONE);
			}else {
				handler.post(runnable);
			}
		}
	};
	
	Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			progressBar1.setProgress(msg.arg1);
		}
		
	};
	

}